package com.watchthelan.web.administrator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.watchthelan.domain.Administrator;
import com.watchthelan.service.administrator.AdministratorManager;

public class AdministratorEditFormController extends SimpleFormController {
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	private AdministratorManager administratorManager;
	
	public ModelAndView onSubmit(Object command) throws ServletException {
		Administrator administrator = (Administrator) command;
		administratorManager.updateAdministrator(administrator);
		return new ModelAndView(new RedirectView(getSuccessView()));
	}
	
	public Object formBackingObject(HttpServletRequest request) throws ServletException {
		logger.info("get administrator");
		Administrator administrator = new Administrator();
		String id = request.getParameter("id");
		if (id != null) {
			administrator = administratorManager.getAdministratorById(new Long(id));
		}
		return administrator;
	}

	public AdministratorManager getAdministratorManager() {
		return administratorManager;
	}

	public void setAdministratorManager(AdministratorManager administratorManager) {
		this.administratorManager = administratorManager;
	}
	

}
